home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / example / ImageMapArea.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  11.3 KB  |  345 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)ImageMapArea.java    1.3 95/10/13  
  19.  *
  20.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  24.  * without fee is hereby granted. 
  25.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  26.  * for further important copyright and trademark information and to
  27.  * http://java.sun.com/licensing.html for further important licensing
  28.  * information for the Java (tm) Technology.
  29.  * 
  30.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  31.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  32.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  34.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  35.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  36.  * 
  37.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  38.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  39.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  40.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  41.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  42.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  43.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  44.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  45.  * HIGH RISK ACTIVITIES.
  46.  */
  47.  
  48. import java.awt.Graphics;
  49. import java.awt.Image;
  50. import java.awt.image.*;
  51. import java.util.StringTokenizer;
  52. import java.net.URL;
  53. import java.net.MalformedURLException;
  54.  
  55. /**
  56.  * The base ImageArea class.
  57.  * This class performs the basic functions that most ImageArea
  58.  * classes will need and delegates specific actions to the subclasses.
  59.  *
  60.  * @author     Jim Graham
  61.  * @version     1.3, 13 Oct 1995
  62.  */
  63. class ImageMapArea implements ImageObserver {
  64.     /** The applet parent that contains this ImageArea. */
  65.     ImageMap parent;
  66.     /** The X location of the area (if rectangular). */
  67.     int X;
  68.     /** The Y location of the area (if rectangular). */
  69.     int Y;
  70.     /** The size().width of the area (if rectangular). */
  71.     int W;
  72.     /** The size().height of the area (if rectangular). */
  73.     int H;
  74.     /**
  75.      * This flag indicates whether the user was in this area during the
  76.      * last scan of mouse locations.
  77.      */
  78.     boolean entered = false;
  79.     /** This flag indicates whether the area is currently highlighted. */
  80.     boolean active = false;
  81.  
  82.     /**
  83.      * This is the default highlight image if no special effects are
  84.      * needed to draw the highlighted image.  It is created by the
  85.      * default "makeImages()" method.
  86.      */
  87.     Image hlImage;
  88.  
  89.     /**
  90.      * Initialize this ImageArea as called from the applet.
  91.      * If the subclass does not override this initializer, then it
  92.      * will perform the basic functions of setting the parent applet
  93.      * and parsing out 4 numbers from the argument string which specify
  94.      * a rectangular region for the ImageArea to act on.
  95.      * The remainder of the argument string is passed to the handleArg()
  96.      * method for more specific handling by the subclass.
  97.      */
  98.     public void init(ImageMap parent, String args) {
  99.     this.parent = parent;
  100.     StringTokenizer st = new StringTokenizer(args, ", ");
  101.     X = Integer.parseInt(st.nextToken());
  102.     Y = Integer.parseInt(st.nextToken());
  103.     W = Integer.parseInt(st.nextToken());
  104.     H = Integer.parseInt(st.nextToken());
  105.     if (st.hasMoreTokens()) {
  106.         // hasMoreTokens() Skips the trailing comma
  107.         handleArg(st.nextToken(""));
  108.     } else {
  109.         handleArg(null);
  110.     }
  111.     makeImages();
  112.     }
  113.  
  114.     /**
  115.      * This method handles the remainder of the argument string after
  116.      * the standard initializer has parsed off the 4 rectangular
  117.      * parameters.  If the subclass does not override this method,
  118.      * the remainder will be ignored.
  119.      */
  120.     public void handleArg(String s) {
  121.     }
  122.  
  123.     /**
  124.      * This method loads any additional media that the ImageMapArea
  125.      * may need for its animations.
  126.      */
  127.     public void getMedia() {
  128.     }
  129.  
  130.     /**
  131.      * This method is called every animation cycle if there are any
  132.      * active animating areas.
  133.      * @return true if this area requires further animation notifications
  134.      */
  135.     public boolean animate() {
  136.     return false;
  137.     }
  138.  
  139.     /**
  140.      * This method sets the image to be used to render the ImageArea
  141.      * when it is highlighted.
  142.      */
  143.     public void setHighlight(Image img) {
  144.     hlImage = img;
  145.     }
  146.  
  147.     /**
  148.      * This method handles the construction of the various images
  149.      * used to highlight this particular ImageArea when the user
  150.      * interacts with it.
  151.      */
  152.     public void makeImages() {
  153.     setHighlight(parent.getHighlight(X, Y, W, H));
  154.     }
  155.  
  156.     /**
  157.      * The repaint method causes the area to be repainted at the next
  158.      * opportunity.
  159.      */
  160.     public void repaint() {
  161.     parent.repaint(0, X, Y, W, H);
  162.     }
  163.  
  164.     /**
  165.      * This method tests to see if a point is inside this ImageArea.
  166.      * The standard method assumes a rectangular area as parsed by
  167.      * the standard initializer.  If a more complex area is required
  168.      * then this method will have to be overridden by the subclass.
  169.      */
  170.     public boolean inside(int x, int y) {
  171.     return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
  172.     }
  173.  
  174.     /**
  175.      * This utility method draws a rectangular subset of a highlight
  176.      * image.
  177.      */
  178.     public void drawImage(Graphics g, Image img, int imgx, int imgy,
  179.               int x, int y, int w, int h) {
  180.     Graphics ng = g.create();
  181.     ng.clipRect(x, y, w, h);
  182.     ng.drawImage(img, imgx, imgy, this);
  183.     }
  184.  
  185.     /**
  186.      * This method handles the updates from drawing the images.
  187.      */
  188.     public boolean imageUpdate(Image img, int infoflags,
  189.                    int x, int y, int width, int height) {
  190.     if (img == hlImage) {
  191.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  192.                       width, height);
  193.     } else {
  194.         return (infoflags & (ALLBITS | ERROR)) == 0;
  195.     }
  196.     }
  197.  
  198.     /**
  199.      * This utility method shows a string in the status bar.
  200.      */
  201.     public void showStatus(String msg) {
  202.     parent.getAppletContext().showStatus(msg);
  203.     }
  204.  
  205.     /**
  206.      * This utility method tells the browser to visit a URL.
  207.      */
  208.     public void showDocument(URL u) {
  209.     parent.getAppletContext().showDocument(u);
  210.     }
  211.  
  212.     /**
  213.      * This method highlights the specified area when the user enters
  214.      * it with his mouse.  The standard highlight method is to replace
  215.      * the indicated rectangular area of the image with the primary
  216.      * highlighted image.
  217.      */
  218.     public void highlight(Graphics g) {
  219.     }
  220.  
  221.     /**
  222.      * The checkEnter method is called when the mouse is inside the
  223.      * region to see if the area needs to have its enter method called.
  224.      * The default implementation simply checks if the entered flag is
  225.      * set and only calls enter if it is false.
  226.      */
  227.     public boolean checkEnter(int x, int y) {
  228.     if (entered) {
  229.         return isTerminal();
  230.     } else {
  231.         entered = true;
  232.         return enter(x, y);
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * The checkExit method is called when the mouse is outside the
  238.      * region to see if the area needs to have its exit method called.
  239.      * The default implementation simply checks if the entered flag is
  240.      * set and only calls exit if it is true.
  241.      */
  242.     public void checkExit() {
  243.     if (entered) {
  244.         entered = false;
  245.         exit();
  246.     }
  247.     }
  248.  
  249.     /**
  250.      * The isTerminal method controls whether events propagate to the
  251.      * areas which lie beneath this one.
  252.      * @return true if the events should be propagated to the underlying
  253.      * areas.
  254.      */
  255.     public boolean isTerminal() {
  256.     return false;
  257.     }
  258.  
  259.     /**
  260.      * The enter method is called when the mouse enters the region.
  261.      * The location is supplied, but the standard implementation is
  262.      * to call the overloaded method with no arguments.
  263.      * @return true if this ImageMapArea wants to prevent any underlying
  264.      * areas from seeing the enter
  265.      */
  266.     public boolean enter(int x, int y) {
  267.     return enter();
  268.     }
  269.  
  270.     /**
  271.      * The overloaded enter method is called when the mouse enters
  272.      * the region.  This method can be overridden if the ImageArea
  273.      * does not need to know where the mouse entered.
  274.      * @return true if this ImageMapArea wants to prevent any underlying
  275.      * areas from seeing the enter
  276.      */
  277.     public boolean enter() {
  278.     return isTerminal();
  279.     }
  280.  
  281.     /**
  282.      * The exit method is called when the mouse leaves the region.
  283.      */
  284.     public void exit() {
  285.     }
  286.  
  287.     /**
  288.      * The press method is called when the user presses the mouse
  289.      * button inside the ImageArea.  The location is supplied, but
  290.      * the standard implementation is to call the overloaded method
  291.      * with no arguments.
  292.      * @return true if this ImageMapArea wants to prevent any underlying
  293.      * areas from seeing the press
  294.      */
  295.     public boolean press(int x, int y) {
  296.     return press();
  297.     }
  298.  
  299.     /**
  300.      * The overloaded press method is called when the user presses the
  301.      * mouse button inside the ImageArea.  This method can be overridden
  302.      * if the ImageArea does not need to know the location of the press.
  303.      * @return true if this ImageMapArea wants to prevent any underlying
  304.      * areas from seeing the press
  305.      */
  306.     public boolean press() {
  307.     return isTerminal();
  308.     }
  309.  
  310.     /**
  311.      * The lift method is called when the user releases the mouse button.
  312.      * The location is supplied, but the standard implementation is to
  313.      * call the overloaded method with no arguments.  Only those ImageAreas
  314.      * that were informed of a press will be informed of the corresponding
  315.      * release.
  316.      * @return true if this ImageMapArea wants to prevent any underlying
  317.      * areas from seeing the lift
  318.      */
  319.     public boolean lift(int x, int y) {
  320.     return lift();
  321.     }
  322.  
  323.     /**
  324.      * The overloaded lift method is called when the user releases the
  325.      * mouse button.  This method can be overridden if the ImageArea
  326.      * does not need to know the location of the release.
  327.      * @return true if this ImageMapArea wants to prevent any underlying
  328.      * areas from seeing the lift
  329.      */
  330.     public boolean lift() {
  331.     return isTerminal();
  332.     }
  333.  
  334.     /**
  335.      * The drag method is called when the user moves the mouse while
  336.      * the button is pressed.  Only those ImageAreas that were informed
  337.      * of a press will be informed of the corresponding mouse movements.
  338.      * @return true if this ImageMapArea wants to prevent any underlying
  339.      * areas from seeing the drag
  340.      */
  341.     public boolean drag(int x, int y) {
  342.     return isTerminal();
  343.     }
  344. }
  345.